package com.example.mybatisdemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.mybatisdemo.config.DynamicDataSourceHolder;
import com.example.mybatisdemo.domain.entity.*;
import com.example.mybatisdemo.domain.vo.DetailsVo;
import com.example.mybatisdemo.domain.vo.MdOuVo;
import com.example.mybatisdemo.domain.vo.RmResourcesVo;
import com.example.mybatisdemo.mapper.*;
import com.example.mybatisdemo.remoteapi.RemoteApi;
import com.example.mybatisdemo.utils.FileDown;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@Slf4j
public class SugetongSyncData {

    @Resource
    private RmResourcesMapper rmResourcesMapper;
    @Resource
    private LandLabelMapper landLabelMapper;
    @Resource
    private LandMapper landMapper;
    @Resource
    private LandKeyPlaceBuildingMapper landKeyPlaceBuildingMapper;
    @Resource
    private LandKeyPlaceJmxqMapper landKeyPlaceJmxqMapper;
    @Resource
    private LandKeyPlaceSchoolMapper landKeyPlaceSchoolMapper;
    @Resource
    private LandKeyPlaceShycsMapper landKeyPlaceShycsMapper;
    @Resource
    private LandKeyPlaceZzzxMapper landKeyPlaceZzzxMapper;
    @Resource
    private LandKeyPointJdwldMapper landKeyPointJdwldMapper;
    @Resource
    private LandKeyPointZjtdMapper landKeyPointZjtdMapper;
    @Resource
    private RemoteApi remoteApi;

    @Resource
    private WuMapper wuMapper;
    @Resource
    private WuLabelMapper wuLabelMapper;
    @Resource
    private WuRfgsMapper wuRfgsMapper;

    @Resource
    private OrgMapper orgMapper;
    @Resource
    private OrgDwdzzMapper dwdzzMapper;
    @Resource
    private OrgFgyzjjzzMapper orgFgyzjjzzMapper;
    @Resource
    private OrgJwfzfzzMapper orgJwfzfzzMapper;
    @Resource
    private  OrgLabelMapper orgLabelMapper;
    @Resource
    private OrgQfqzzzMapper orgQfqzzzMapper;
    @Resource
    private OrgShzzMapper orgShzzMapper;




    @Test
    public void syncOrgData() {
        String downloadDir = System.getProperty("user.dir");

        DynamicDataSourceHolder.setManualDBType("sugetong");
        List<MdOuVo> dataList = rmResourcesMapper.listOrgData();
        for (MdOuVo mdOuVo : dataList) {

            DynamicDataSourceHolder.setManualDBType("master");
            List<Org> list = orgMapper.selectList(new QueryWrapper<Org>().lambda()
                    .eq(Org::getRowId, mdOuVo.getRowId()));
            if (CollectionUtils.isNotEmpty(list)) {
                log.info("已存在该数据：{}", mdOuVo.getOrgNm());
                continue;
            }
            log.info("存数据：{}", mdOuVo.getRowId());

            Org entity = new Org();
            entity.setRowId(mdOuVo.getRowId());
            entity.setName(mdOuVo.getOrgNm());
            entity.setCode(mdOuVo.getOrgSnm());
            entity.setAddress(mdOuVo.getAddr());
            entity.setLngLat(mdOuVo.getLon() + "," + mdOuVo.getLat());
            DynamicDataSourceHolder.setManualDBType("sugetong");
            List<String> mainFileList = rmResourcesMapper.listAttachment(mdOuVo.getRowId());
            List<String> fileList = new ArrayList<>();
            for (String path : mainFileList) {
                try {
                    String fileName = path.substring(path.lastIndexOf('/') + 1);
                    File file = FileDown.downloadFile(path, downloadDir, fileName);
                    MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                    String uploadRes = remoteApi.uploadFile(multipartFile);
                    String data = JSON.parseObject(uploadRes).getString("data");
                    fileList.add(data);
                    file.delete();
                } catch (Exception e) {
                    log.error("下载文件失败：{}", e.getMessage());
                }

            }
            entity.setFileUrl(StringUtils.join(fileList, ","));

            JSONObject gridData = rmResourcesMapper.getGridData(mdOuVo.getGridId());
            if (gridData != null) {
                entity.setWgCode(gridData.getString("code"));
            }
            entity.setContact(mdOuVo.getHead());
            entity.setPhone(mdOuVo.getHead_tel());
            entity.setXiaQu(mdOuVo.getRegionCd());

            //查询明细
            List<DetailsVo> detailsVoList = rmResourcesMapper.getDetails(mdOuVo.getRowId());
            JSONObject details = new JSONObject();
            for (DetailsVo detailsVo : detailsVoList) {
                if (detailsVo.getInputType().equals("select")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    String code = getDictCodeByValue(val);
                    details.put(detailsVo.getTitle(), code);
                } else if (detailsVo.getInputType().equals("radio")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    details.put(detailsVo.getTitle(), val);
                } else if (detailsVo.getInputType().equals("multiselect")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    if (StringUtils.isBlank(details.getString(detailsVo.getTitle()))) {
                        details.put(detailsVo.getTitle(), val);
                    } else {
                        details.put(detailsVo.getTitle(), details.getString(detailsVo.getTitle()) + "-" + val);
                    }

                } else if (detailsVo.getInputType().equals("photo")) {
                    List<String> pathList = rmResourcesMapper.listAttachment(detailsVo.getRowId());
                    List<String> urlList = new ArrayList<>();
                    for (String path : pathList) {
                        try {
                            String fileName = path.substring(path.lastIndexOf('/') + 1);
                            File file = FileDown.downloadFile(path, downloadDir, fileName);
                            MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                            String uploadRes = remoteApi.uploadFile(multipartFile);
                            String data = JSON.parseObject(uploadRes).getString("data");
                            urlList.add(data);
                            file.delete();
                        } catch (Exception e) {
                            log.error("下载文件失败：{}", e.getMessage());
                        }
                    }
                    details.put(detailsVo.getTitle(), StringUtils.join(urlList, ","));
                } else {
                    details.put(detailsVo.getTitle(), detailsVo.getVal());
                }
            }

            //标签
            List<String> labelList = rmResourcesMapper.listOrgLabel(mdOuVo.getRowId());
            entity.setClrq(details.getString("成立日期"));
            entity.setFddbrxm(details.getString("法定代表人姓名"));
            entity.setFddbrzjzl(details.getString("法定代表人证件种类"));
            entity.setFddbrzjhm(details.getString("法定代表人证件号码"));
            entity.setBriefIntroduction(details.getString("备注"));
            DynamicDataSourceHolder.setManualDBType("master");
            orgMapper.insert(entity);

            for (String label : labelList) {
                OrgLabel labelEntity = new OrgLabel();
                labelEntity.setOrgId(entity.getId());
                labelEntity.setLabelCode(getDictCodeByValue(label));
                orgLabelMapper.insert(labelEntity);
            }
            if (labelList.contains("社会组织")) {
                OrgShzz orgShzz = new OrgShzz();
                orgShzz.setOrgId(entity.getId());
                orgShzz.setCjrq(details.getString("创建日期"));
                orgShzz.setDjzh(details.getString("登记证号"));
                orgShzz.setDjgljgdm(details.getString("登记管理机关代码"));
                orgShzz.setShzzlx(details.getString("社会组织类型"));
                orgShzz.setYjwbj(details.getString("有境外背景"));
                orgShzz.setGzcd(details.getString("关注程度"));
                orgShzz.setBwfzrxm(details.getString("保卫负责人姓名"));
                orgShzz.setBwfzrsfzh(details.getString("保卫负责人身份证号"));
                orgShzz.setBwfzrlxdh(details.getString("保卫负责人联系电话"));
                orgShzz.setZgdys(details.getString("中共党员数"));
                orgShzz.setSfyzgdzz(details.getString("是否有中共党组织"));
                orgShzz.setSfjbjlzgdzz(details.getString("是否具备建立中共党组织条件"));
                orgShzz.setSfygqty(details.getString("是否有共青团组织"));
                orgShzz.setGqtys(details.getString("共青团员数"));
                orgShzz.setSfygh(details.getString("是否有工会"));
                orgShzz.setGhrys(details.getString("工会人员数"));
                orgShzz.setYflzz(details.getString("有妇联组织"));
                orgShzz.setFlrys(details.getString("妇联人员数"));
                orgShzzMapper.insert(orgShzz);
            }
            if (labelList.contains("单位党组织")) {
                OrgDwdzz orgDwdzz = new OrgDwdzz();
                orgDwdzz.setOrgId(entity.getId());
                orgDwdzz.setDzzbm(details.getString("党组织编码"));
                orgDwdzz.setDzzmc(details.getString("党组织名称"));
                orgDwdzz.setSjdzzbm(details.getString("上级党组织编码"));
                orgDwdzz.setSjdzzmc(details.getString("上级党组织名称"));
                orgDwdzz.setDyrs(details.getString("党员人数"));
                orgDwdzz.setDzzsjxm(details.getString("党组织书记姓名"));
                orgDwdzz.setDzzsjsfzh(details.getString("党组织书记身份证号"));
                orgDwdzz.setDzzsjdh(details.getString("党组织书记电话"));
                orgDwdzz.setSsdw(details.getString("所属单位"));
                dwdzzMapper.insert(orgDwdzz);
            }

            if (labelList.contains("非公有制经济组织")) {
                OrgFgyzjjzz fgyzjjzz = new OrgFgyzjjzz();
                fgyzjjzz.setOrgId(entity.getId());
                fgyzjjzz.setZzjc(details.getString("组织简称"));
                fgyzjjzz.setZzywmc(details.getString("组织英文名称"));
                fgyzjjzz.setZzywsx(details.getString("组织英文缩写"));
                fgyzjjzz.setZzdh(details.getString("组织电话"));
                fgyzjjzz.setZzczh(details.getString("组织传真号"));
                fgyzjjzz.setZzdzyx(details.getString("组织电子邮箱"));
                fgyzjjzz.setZzlb(details.getString("组织类别"));
                fgyzjjzz.setZzlx(details.getString("组织类型"));
                fgyzjjzz.setZgbmmc(details.getString("主管部门名称"));
                fgyzjjzz.setJjxz(details.getString("经济性质"));
                fgyzjjzz.setJjlb(details.getString("经济类别"));
                fgyzjjzz.setJjfw(details.getString("经济范围"));
                fgyzjjzz.setCyrs(details.getString("从业人数"));
                fgyzjjzz.setFzrq(details.getString("发照日期"));
                fgyzjjzz.setKyrq(details.getString("开业日期"));
                fgyzjjzz.setZcrq(details.getString("注册日期"));
                fgyzjjzz.setZczb(details.getString("注册资本（万元）"));
                fgyzjjzz.setDwdz(details.getString("单位地址"));
                fgyzjjzz.setDszxm(details.getString("董事长姓名"));
                fgyzjjzz.setDszsfzh(details.getString("董事长身份证号"));
                fgyzjjzz.setDszlxdh(details.getString("董事长电话"));
                fgyzjjzz.setZjlxm(details.getString("总经理姓名"));
                fgyzjjzz.setZjlsfzh(details.getString(" 总经理身份证号"));
                fgyzjjzz.setZjllxdh(details.getString("总经理电话"));
                fgyzjjzz.setBwfzrxm(details.getString("保卫负责人姓名"));
                fgyzjjzz.setBwfzrsfzh(details.getString("保卫负责人身份证号"));
                fgyzjjzz.setBwfzrlxdh(details.getString("保卫负责人电话"));
                fgyzjjzz.setWhqy(details.getString("危化企业"));
                fgyzjjzz.setZddw(details.getString("重点单位"));
                fgyzjjzz.setAqyhlx(details.getString("安全隐患类型"));
                fgyzjjzz.setGzcd(details.getString("关注程度"));
                fgyzjjzz.setZgdys(details.getString(" 中共党员数"));
                fgyzjjzz.setSfyzgdzz(details.getString("是否有中共党组织"));
                fgyzjjzz.setSsjbjlzgdzztj(details.getString("是否具备建立中共党组织条件"));
                fgyzjjzz.setSfyfqty(details.getString("是否有共青团组织"));
                fgyzjjzz.setGqtys(details.getString("共青团员数"));
                fgyzjjzz.setSfygh(details.getString("是否有工会"));
                fgyzjjzz.setGhrys(details.getString("工会人员数"));
                fgyzjjzz.setSfyflzz(details.getString("是否有妇联组织"));
                fgyzjjzz.setFlrys(details.getString("妇联人员数"));
                orgFgyzjjzzMapper.insert(fgyzjjzz);
            }
            if (labelList.contains("群防群治组织")) {
                OrgQfqzzz qfqzzz = new OrgQfqzzz();
                qfqzzz.setOrgId(entity.getId());
                qfqzzz.setZzlx(details.getString("组织类型"));
                qfqzzz.setYwzdbm(details.getString("业务指导部门"));
                qfqzzz.setZzrysl(details.getString("组织人员数量"));
                qfqzzz.setZyznjygk(details.getString("主要职能简要概况"));
                orgQfqzzzMapper.insert(qfqzzz);
            }

            if (labelList.contains("境外非政府组织")) {
                OrgJwfzfzz orgJwfzfzz = new OrgJwfzfzz();
                orgJwfzfzz.setOrgId(entity.getId());
                orgJwfzfzz.setSszz(details.getString("所属组织"));
                orgJwfzfzz.setZzssgjdq(details.getString("组织所属国家地区"));
                orgJwfzfzz.setJnfzrxm(details.getString("境内负责人姓名"));
                orgJwfzfzz.setJnfzrsfzh(details.getString("境内负责人身份证号"));
                orgJwfzfzz.setJnfzrlxfs(details.getString("境内负责人联系方式"));
                orgJwfzfzz.setCzcfhnwfryxm(details.getString("常驻/常赴境内外方人员姓名"));
                orgJwfzfzz.setCzcfhnwfryzjmc(details.getString("常驻/常赴境内外方人员证件名称"));
                orgJwfzfzz.setCzcfhnwfryzjh(details.getString("常驻/常赴境内外方人员证件号"));
                orgJwfzfzz.setCzcfhnwfrylxfs(details.getString("常驻/常赴境内外方人员联系方式"));
                orgJwfzfzz.setZyjynr(details.getString("主要经营内容"));
                orgJwfzfzz.setCpyxfx(details.getString("产品营销方向"));
               orgJwfzfzzMapper.insert(orgJwfzfzz);
            }
        }


    }

    @Test
    public void syncWuData() {
        String downloadDir = System.getProperty("user.dir");

        DynamicDataSourceHolder.setManualDBType("sugetong");
        List<RmResourcesVo> dataList = rmResourcesMapper.listData();
        for (RmResourcesVo rmResourcesVo : dataList) {

            DynamicDataSourceHolder.setManualDBType("master");
            List<Wu> list = wuMapper.selectList(new QueryWrapper<Wu>().lambda()
                    .eq(Wu::getRowId, rmResourcesVo.getRowId()));
            if (CollectionUtils.isNotEmpty(list)) {
                log.info("已存在该数据：{}", rmResourcesVo.getName());
                continue;
            }
            log.info("存数据：{}", rmResourcesVo.getRowId());

            Wu entity = new Wu();
            entity.setRowId(rmResourcesVo.getRowId());
            entity.setName(rmResourcesVo.getName());
            entity.setAddress(rmResourcesVo.getAddress());
            entity.setLgtLat(rmResourcesVo.getLongitude() + "," + rmResourcesVo.getLatitude());
            entity.setDescription(rmResourcesVo.getNotes());
            DynamicDataSourceHolder.setManualDBType("sugetong");
            List<String> mainFileList = rmResourcesMapper.listAttachment(rmResourcesVo.getRowId());
            List<String> fileList = new ArrayList<>();
            for (String path : mainFileList) {
                try {
                    String fileName = path.substring(path.lastIndexOf('/') + 1);
                    File file = FileDown.downloadFile(path, downloadDir, fileName);
                    MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                    String uploadRes = remoteApi.uploadFile(multipartFile);
                    String data = JSON.parseObject(uploadRes).getString("data");
                    fileList.add(data);
                    file.delete();
                } catch (Exception e) {
                    log.error("下载文件失败：{}", e.getMessage());
                }

            }
            entity.setFileUrl(StringUtils.join(fileList, ","));

            JSONObject gridData = rmResourcesMapper.getGridData(rmResourcesVo.getGridId());
            if (gridData != null) {
                entity.setWgCode(gridData.getString("code"));
            }

            //查询明细
            List<DetailsVo> detailsVoList = rmResourcesMapper.getDetails(rmResourcesVo.getRowId());
            JSONObject details = new JSONObject();
            for (DetailsVo detailsVo : detailsVoList) {
                if (detailsVo.getInputType().equals("select")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    String code = getDictCodeByValue(val);
                    details.put(detailsVo.getTitle(), code);
                } else if (detailsVo.getInputType().equals("radio")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    details.put(detailsVo.getTitle(), val);
                } else if (detailsVo.getInputType().equals("multiselect")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    if (StringUtils.isBlank(details.getString(detailsVo.getTitle()))) {
                        details.put(detailsVo.getTitle(), val);
                    } else {
                        details.put(detailsVo.getTitle(), details.getString(detailsVo.getTitle()) + "-" + val);
                    }

                } else if (detailsVo.getInputType().equals("photo")) {
                    List<String> pathList = rmResourcesMapper.listAttachment(detailsVo.getRowId());
                    List<String> urlList = new ArrayList<>();
                    for (String path : pathList) {
                        try {
                            String fileName = path.substring(path.lastIndexOf('/') + 1);
                            File file = FileDown.downloadFile(path, downloadDir, fileName);
                            MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                            String uploadRes = remoteApi.uploadFile(multipartFile);
                            String data = JSON.parseObject(uploadRes).getString("data");
                            urlList.add(data);
                            file.delete();
                        } catch (Exception e) {
                            log.error("下载文件失败：{}", e.getMessage());
                        }
                    }
                    details.put(detailsVo.getTitle(), StringUtils.join(urlList, ","));
                } else {
                    details.put(detailsVo.getTitle(), detailsVo.getVal());
                }
            }

            entity.setContact(details.getString("联系人"));
            entity.setContactPhone(details.getString("联系电话"));
            //标签
            List<String> labelList = rmResourcesMapper.listLabel(rmResourcesVo.getRowId());
            DynamicDataSourceHolder.setManualDBType("master");
            wuMapper.insert(entity);

            for (String label : labelList) {
                WuLabel labelEntity = new WuLabel();
                labelEntity.setWuId(entity.getId());
                labelEntity.setLabelCode(getDictCodeByValue(label));
                wuLabelMapper.insert(labelEntity);
            }
            if (labelList.contains("人防工事")) {
                WuRfgs rfgs = new WuRfgs();
                rfgs.setWuId(entity.getId());
                rfgs.setXqpmtp(details.getString("小区平面照片"));
                rfgs.setSfyxfxcl(details.getString("是否有消防宣传栏"));
                rfgs.setSfyddcjztfdzp(details.getString("是否有电动车集中充电停放点"));
                rfgs.setSfywxxfz(details.getString("是否有微型消防站"));
                rfgs.setXfxclzp(details.getString("消防宣传栏照片"));
                rfgs.setDdcjztfdzp(details.getString("电动车集中充电停放点照片"));
                rfgs.setWxxfzzp(details.getString("微型消防站照片"));
                wuRfgsMapper.insert(rfgs);
            }
        }


    }

    @Test
    public void syncLandData() {
        String downloadDir = System.getProperty("user.dir");

        DynamicDataSourceHolder.setManualDBType("sugetong");
        List<RmResourcesVo> dataList = rmResourcesMapper.listData();
        for (RmResourcesVo rmResourcesVo : dataList) {

            DynamicDataSourceHolder.setManualDBType("master");
            List<Land> lands = landMapper.selectList(new QueryWrapper<Land>().lambda()
                    .eq(Land::getRowId, rmResourcesVo.getRowId()));
            if (CollectionUtils.isNotEmpty(lands)) {
                log.info("已存在该数据：{}", rmResourcesVo.getName());
                continue;
            }
            log.info("存数据：{}", rmResourcesVo.getRowId());

            Land land = new Land();
            land.setName(rmResourcesVo.getName());
            land.setAddress(rmResourcesVo.getAddress());
            land.setLgtLat(rmResourcesVo.getLongitude() + "," + rmResourcesVo.getLatitude());
            land.setDescription(rmResourcesVo.getNotes());
            DynamicDataSourceHolder.setManualDBType("sugetong");
            List<String> mainFileList = rmResourcesMapper.listAttachment(rmResourcesVo.getRowId());
            List<String> fileList = new ArrayList<>();
            for (String path : mainFileList) {
                try {
                    String fileName = path.substring(path.lastIndexOf('/') + 1);
                    File file = FileDown.downloadFile(path, downloadDir, fileName);
                    MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                    String uploadRes = remoteApi.uploadFile(multipartFile);
                    String data = JSON.parseObject(uploadRes).getString("data");
                    fileList.add(data);
                    file.delete();
                } catch (Exception e) {
                    log.error("下载文件失败：{}", e.getMessage());
                }

            }
            land.setFileUrl(StringUtils.join(fileList, ","));

            JSONObject gridData = rmResourcesMapper.getGridData(rmResourcesVo.getGridId());
            if (gridData != null) {
                land.setWgCode(gridData.getString("code"));
            }

            //查询明细
            List<DetailsVo> detailsVoList = rmResourcesMapper.getDetails(rmResourcesVo.getRowId());
            JSONObject details = new JSONObject();
            for (DetailsVo detailsVo : detailsVoList) {
                if (detailsVo.getInputType().equals("select")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    String code = getDictCodeByValue(val);
                    details.put(detailsVo.getTitle(), code);
                } else if (detailsVo.getInputType().equals("radio")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    details.put(detailsVo.getTitle(), val);
                } else if (detailsVo.getInputType().equals("multiselect")) {
                    String val = rmResourcesMapper.getEnumitem(detailsVo.getEnumitemIdr());
                    if (StringUtils.isBlank(details.getString(detailsVo.getTitle()))) {
                        details.put(detailsVo.getTitle(), val);
                    } else {
                        details.put(detailsVo.getTitle(), details.getString(detailsVo.getTitle()) + "-" + val);
                    }

                } else if (detailsVo.getInputType().equals("photo")) {
                    List<String> pathList = rmResourcesMapper.listAttachment(detailsVo.getRowId());
                    List<String> urlList = new ArrayList<>();
                    for (String path : pathList) {
                        try {


                            String fileName = path.substring(path.lastIndexOf('/') + 1);
                            File file = FileDown.downloadFile(path, downloadDir, fileName);
                            MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
                            String uploadRes = remoteApi.uploadFile(multipartFile);
                            String data = JSON.parseObject(uploadRes).getString("data");
                            urlList.add(data);
                            file.delete();
                        } catch (Exception e) {
                            log.error("下载文件失败：{}", e.getMessage());
                        }
                    }
                    details.put(detailsVo.getTitle(), StringUtils.join(urlList, ","));
                } else {
                    details.put(detailsVo.getTitle(), detailsVo.getVal());
                }
            }

            land.setContact(details.getString("联系人"));
            land.setContactPhone(details.getString("联系电话"));
            //标签
            List<String> labelList = rmResourcesMapper.listLabel(rmResourcesVo.getRowId());
            DynamicDataSourceHolder.setManualDBType("master");
            landMapper.insert(land);

            for (String label : labelList) {
                LandLabel landLabel = new LandLabel();
                landLabel.setLandId(land.getId());
                landLabel.setLabelCode(getDictCodeByValue(label));
                landLabelMapper.insert(landLabel);
            }
            if (labelList.contains("建筑物")) {
                LandKeyPlaceBuilding landKeyPlaceBuilding = new LandKeyPlaceBuilding();
                landKeyPlaceBuilding.setLandId(land.getId());
                landKeyPlaceBuilding.setBuildName(details.getString("建筑物别名（简称）"));
                landKeyPlaceBuilding.setJwzrq(details.getString("警务责任区"));
                landKeyPlaceBuilding.setDzyslx(details.getString("地址元素类型"));
                landKeyPlaceBuilding.setDzsfcz(details.getString("地址是否存在"));
                landKeyPlaceBuilding.setDzsfzy(details.getString("地址是否在用"));
                landKeyPlaceBuildingMapper.insert(landKeyPlaceBuilding);
            }
            if (labelList.contains("综治中心")) {
                LandKeyPlaceZzzx landKeyPlaceZzzx = new LandKeyPlaceZzzx();
                landKeyPlaceZzzx.setLandId(land.getId());
                landKeyPlaceZzzx.setZzzxdh(details.getString("综治中心电话"));
                landKeyPlaceZzzx.setZzcj(details.getString("综治层级"));
                landKeyPlaceZzzx.setFzrxm(details.getString("负责人姓名"));
                landKeyPlaceZzzx.setFzrsfzh(details.getString("负责人身份证号"));
                landKeyPlaceZzzx.setFzrdh(details.getString("负责人电话"));
                landKeyPlaceZzzx.setZcdwxxqk(details.getString("组成单位详细情况"));
                landKeyPlaceZzzxMapper.insert(landKeyPlaceZzzx);
            }

            if (labelList.contains("居民小区")) {
                LandKeyPlaceJmxq landKeyPlaceJmxq = new LandKeyPlaceJmxq();
                landKeyPlaceJmxq.setLandId(land.getId());
                landKeyPlaceJmxq.setXqmc(details.getString("小区名称"));
                landKeyPlaceJmxq.setXqdz(details.getString("小区地址"));
                landKeyPlaceJmxq.setMph(details.getString("门（楼）牌号"));
                landKeyPlaceJmxq.setMdz(details.getString("门（楼）地址"));
                landKeyPlaceJmxq.setZzjzsl(details.getString("住宅建筑数量"));
                landKeyPlaceJmxq.setJmhsl(details.getString("居民户数量"));
                landKeyPlaceJmxq.setJmrks(details.getString("居住人口数"));
                landKeyPlaceJmxq.setZdcsmj(details.getString("占地场所面积（m2）"));
                landKeyPlaceJmxq.setWydwmc(details.getString("物业单位名称"));
                landKeyPlaceJmxq.setWyfzrxm(details.getString("物业负责人姓名"));
                landKeyPlaceJmxq.setWyfzrsfzh(details.getString("物业负责人身份证号"));
                landKeyPlaceJmxq.setWyfzrsjh(details.getString("物业负责人手机号"));
                landKeyPlaceJmxq.setYwhfzrxm(details.getString("业委会负责人姓名"));
                landKeyPlaceJmxq.setYwhfzrsfzh(details.getString("业委会负责人身份证号"));
                landKeyPlaceJmxq.setYwhfzrsjh(details.getString("业委会负责人手机号"));
                landKeyPlaceJmxq.setXqpmzp(details.getString("小区平面照"));
                landKeyPlaceJmxq.setSfyxfxcl(details.getString("是否有消防宣传栏"));
                landKeyPlaceJmxq.setXfxclzp(details.getString("消防宣传栏照片"));
                landKeyPlaceJmxq.setSfyddcjzcdtfd(details.getString("是否有电动车集中充电停放点"));
                landKeyPlaceJmxq.setDdcjzcdtfdzp(details.getString("电动车集中充电停放点照片"));
                landKeyPlaceJmxq.setSfywxxfz(details.getString("是否有微型消防站"));
                landKeyPlaceJmxq.setWxxfzzp(details.getString("微型消防站照片"));
                landKeyPlaceJmxqMapper.insert(landKeyPlaceJmxq);
            }
            if (labelList.contains("学校")) {

                LandKeyPlaceSchool landKeyPlaceSchool = new LandKeyPlaceSchool();
                landKeyPlaceSchool.setLandId(land.getId());
                landKeyPlaceSchool.setXxdm(details.getString("学校代码"));
                landKeyPlaceSchool.setBxlx(details.getString("办学类型"));
                landKeyPlaceSchool.setXxxz(details.getString("学校性质"));
                landKeyPlaceSchool.setSszgbm(details.getString("所属主管部门"));
                landKeyPlaceSchool.setZxxsh(details.getString("在校学生数"));
                landKeyPlaceSchool.setXzxm(details.getString("校长姓名"));
                landKeyPlaceSchool.setXzsfz(details.getString("校长身份证号"));
                landKeyPlaceSchool.setXzlxdh(details.getString("校长联系电话"));
                landKeyPlaceSchool.setFgaqbwzrrxm(details.getString("分管安全保卫责任人姓名"));
                landKeyPlaceSchool.setFgaqbwzrrsfzh(details.getString("分管安全保卫责任人身份证号"));
                landKeyPlaceSchool.setFgaqbwzrrdh(details.getString("分管安全保卫责任人电话"));
                landKeyPlaceSchool.setAqbwzrrxm(details.getString("安全保卫责任人姓名"));
                landKeyPlaceSchool.setAqbwzrrdh(details.getString("安全保卫责任人电话"));
                landKeyPlaceSchool.setAqbwzrrsfz(details.getString("安全保卫责任人身份证号"));
                landKeyPlaceSchool.setZafzrxm(details.getString("治安负责人姓名"));
                landKeyPlaceSchool.setZafzrsfzh(details.getString("治安负责人身份证号"));
                landKeyPlaceSchool.setZafzrdh(details.getString("治安负责人电话"));
                landKeyPlaceSchool.setAqbwrs(details.getString("安全保卫人数"));
                landKeyPlaceSchoolMapper.insert(landKeyPlaceSchool);


            }
            if (labelList.contains("三合一场所")) {
                LandKeyPlaceShycs landKeyPlaceShycs = new LandKeyPlaceShycs();
                landKeyPlaceShycs.setLandId(land.getId());
                landKeyPlaceShycs.setCslx(details.getString("场所类型"));
                landKeyPlaceShycs.setJzrs(details.getString("居住人数"));
                landKeyPlaceShycs.setCsnsfymqg(details.getString("场所内是否有煤气罐"));
                landKeyPlaceShycs.setCsnsfpbmhq(details.getString("场所内是否配备灭火器"));
                landKeyPlaceShycs.setCsnsfzsfdc(details.getString("场所内是否装设防盗窗"));
                landKeyPlaceShycs.setSftszg(details.getString("是否提示整改"));
                landKeyPlaceShycs.setCsmtzp(details.getString("场所门头照"));
                landKeyPlaceShycs.setCsyyzh(details.getString("场所营业执照"));
                landKeyPlaceShycsMapper.insert(landKeyPlaceShycs);
            }
            if (labelList.contains("寄递物流点")) {
                LandKeyPointJdwld landKeyPointJdwld = new LandKeyPointJdwld();
                landKeyPointJdwld.setLandId(land.getId());
                landKeyPointJdwld.setTyshxydm(details.getString("统一社会信用代码"));
                landKeyPointJdwld.setDjzclx(details.getString("登记注册类型"));
                landKeyPointJdwld.setFzrxm(details.getString("负责人姓名"));
                landKeyPointJdwld.setFzrsfzh(details.getString("负责人身份证号"));
                landKeyPointJdwld.setFzrdh(details.getString("负责人电话"));
                landKeyPointJdwld.setKgqk(details.getString("控股情况"));
                landKeyPointJdwld.setJyfw(details.getString("经营范围"));
                landKeyPointJdwld.setQylx(details.getString("企业类型"));
                landKeyPointJdwld.setFwpp(details.getString("服务品牌"));
                landKeyPointJdwld.setCyysl(details.getString("从业员数量"));
                landKeyPointJdwld.setJksxjsl(details.getString("监控摄像机数量"));
                landKeyPointJdwld.setXgjsl(details.getString("X光机数量"));
                landKeyPointJdwld.setXyshfx(details.getString("是否落实100%先验视后封箱"));
                landKeyPointJdwld.setJdsmz(details.getString("否落实100%寄递实名制"));
                landKeyPointJdwld.setGjaj(details.getString("是否落实100%X光机安检"));

                landKeyPointJdwldMapper.insert(landKeyPointJdwld);
            }

            if (labelList.contains("宗教堂点")) {
                LandKeyPointZjtd landKeyPointZjtd = new LandKeyPointZjtd();
                landKeyPointZjtd.setLandId(land.getId());
                landKeyPointZjtd.setZjxy(details.getString("宗教信仰"));
                landKeyPointZjtd.setFzrxm(details.getString("负责人姓名"));
                landKeyPointZjtd.setFzrsfzh(details.getString("负责人身份证号"));
                landKeyPointZjtd.setFzrdh(details.getString("负责人电话"));
                landKeyPointZjtdMapper.insert(landKeyPointZjtd);
            }
        }


    }

    private String getDictCodeByValue(String val) {
        try {
            String dictRes = remoteApi.getDictByValue(val);
            String data = JSON.parseObject(dictRes).getString("data");
            String code = JSON.parseObject(data).getString("code");
            return code;
        } catch (Exception e) {
            log.error("获取字典失败", e);
            return null;
        }

    }

    @Test
    public void test1() {
        String downloadDir = System.getProperty("user.dir");
        String path = "http://2.198.153.12:82/uploads/20200324/15850343417114.jpg";
        //http://2.198.153.12:82/uploads/20241021/17295044201279.jpg
        String fileName = path.substring(path.lastIndexOf('/') + 1);
        File file = FileDown.downloadFile(path, downloadDir, fileName);
        MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
        String uploadRes = remoteApi.uploadFile(multipartFile);
        String data = JSON.parseObject(uploadRes).getString("data");
        file.delete();
    }

    @Test
    public void test2() {
        String suiBanUserPhoneList = "[18262991767]";
        JSONObject params = new JSONObject();
        params.put("title", "会议");
        params.put("content", "请登录预警信息管理平台审核！");
        params.put("userList", JSON.parseArray(suiBanUserPhoneList,String.class));
        params.put("appParams", new JSONObject());
        params.put("accessKey", "uZPPK7IPFFVHb4VohSDj");
        params.put("secretKey", "5gn1c7fQCDlbSKtYM1ZLgA==");
        params.put("openTeamId", "abca8b957a2eb773aaf8e3dedfa8ddc1");
        remoteApi.sendSuiBanMsg(params);
    }

}
